<template>
	<view class="container">
		<view class="logo">
			<image src="https://mx.acktechnologies.com.cn/uploads/20210709/b6bb59a54081157543d92044b2ac7378.png"
				mode=""></image>
		</view>
		<view class="name">
			明心教育
		</view>
		<view class="company">
			洛阳问学学生校外托管服务有限公司
		</view>
		<view class="desc">
			<view>
				该程序需获得以下授权：
			</view>
			<view>
				*获取您的手机号
			</view>
		</view>
		<button class="auth" hover-class='none' open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
			允许
		</button>
		<button class="cancel" hover-class='none' @click="cancel">
			取消
		</button>
		<view class="agree xy_center">
			<text>允许授权代表您已同意</text>
			<navigator url="../my/tool/privacy">用户隐私协议</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: "",
				session_key: '',
				openid: '',
				jump: ''
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.jump = option.redirect; //打印出上个页面传递的参数。
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					console.log(loginRes)
					this.code = loginRes.code;
					this.$request('/api/wxapp/index/wxLogin', {
						'code': loginRes.code
					}).then(wxLoginRes => {
						if (wxLoginRes.code === 1) {
							this.isReg = wxLoginRes.data.is_reg;
							this.session_key = wxLoginRes.data.session_key;
							this.openid = wxLoginRes.data.openid;
							if (this.isReg&&this.jump) {
								let param = {
									'openid': this.openid
								}
								this.$request('/api/wxapp/index/accountLogin',
											param)
										.then(res => {
											let userinfo = res.data.userinfo;
											console.log(userinfo)
											uni.setStorage({
												key: 'user_info',
												data: userinfo,
												success: function() {
													console.log('success');
												}
											});
											if (res.data.usernum > 1) {
												uni.navigateTo({
													url: './user_select'
												});
											} else {
												uni.showToast({
													title: res.msg,
													duration: 2000,
													icon: 'none',
												});
												if(this.jump){
													if (this.jump.indexOf('my/index') != -1) {
														uni.switchTab({
															url: this.jump
														})
													} else {
														uni.redirectTo({
															url: this.jump
														})
													}
												}else{
													uni.switchTab({
														url: '../index/index'
													})
												}
											}
										})
								
								// if(this.jump){
								// 	if (this.jump.indexOf('my/index') != -1) {
								// 		uni.switchTab({
								// 			url: this.jump
								// 		})
								// 	} else {
								// 		uni.redirectTo({
								// 			url: this.jump
								// 		})
								// 	}
								// }else{
								// 	uni.switchTab({
								// 		url: '../index/index'
								// 	})
								// }
								
							}
						}
					})
				}
			});
		},
		created() {

		},
		methods: {
			cancel() {
				uni.switchTab({
					url: '../index/index'
				});
			},
			onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					//拒绝授权后弹出一些提示  
					uni.showToast({
						title: '您拒绝了授权登录 ！',
						duration: 2000,
						icon: 'none',
					});
				} else { //允许授权  
					console.log(e.detail.encryptedData)
					e.detail.encryptedData //加密的用户信息  
					e.detail.iv //加密算法的初始向量  时要用到  
					this.$request('/api/wxapp/index/decodePhone', {
						"session_key": this.session_key,
						"encryptedData": e.detail.encryptedData,
						"iv": e.detail.iv
					}, 'POST', {
						'content-type': 'application/x-www-form-urlencoded'
					}).then(res => {
						console.log(res)
						if (res.code === 1) {
							let param = {
								'openid': this.openid
							}
							if (!this.isReg) {
								param.mobile = res.data.phone;
							}
							this.$request('/api/wxapp/index/accountLogin',
									param)
								.then(res => {
									let userinfo = res.data.userinfo;
									console.log(userinfo)
									uni.setStorage({
										key: 'user_info',
										data: userinfo,
										success: function() {
											console.log('success');
										}
									});
									if (res.data.usernum > 1) {
										uni.navigateTo({
											url: './user_select'
										});
									} else {
										uni.showToast({
											title: res.msg,
											duration: 2000,
											icon: 'none',
										});
										if(this.jump){
											if (this.jump.indexOf('my/index') != -1) {
												uni.switchTab({
													url: this.jump
												})
											} else {
												uni.redirectTo({
													url: this.jump
												})
											}
										}else{
											uni.switchTab({
												url: '../index/index'
											});
										}
									}
								})
						} else {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: 'none',
							});
						}
					})

				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100%;
		height: 100%;
		overflow: hidden;
		text-align: center;
		background-color: #f0f2f5;
		font-family: SourceHanSansCN-Normal, SourceHanSansCN;
	}

	.logo {
		width: 60px;
		height: 60px;
		margin: 64px auto 39px auto;

		image {
			width: 60px;
			height: 60px;
			border-radius: 50%;
		}
	}

	.name {
		font-size: 17px;
		color: #333333;
		margin-bottom: 20px;
	}

	.company {
		font-size: 15px;
		color: #999999;
	}

	.desc {
		width: 714rpx;
		margin: 25px auto;
		text-align: left;
		padding: 20px 11px;
		border-top: 1px solid rgba(151, 151, 151, 0.19);
		box-sizing: border-box;
		font-size: 15px;
		color: #333333;

		view:nth-child(2) {
			margin-top: 20px;
		}
	}

	button {
		width: 270px;
		height: 44px;
		line-height: 44px;
		font-size: 20px;
		border-radius: 22px;
		cursor: pointer;
	}

	.auth {
		background: #16C05E;
		color: #FFFFFF;
	}

	.cancel {
		background: #EDEDED;
		color: #666666;
		margin-top: 20px;
	}

	button::after {
		border: none;
	}

	.agree {
		margin-top: 50rpx;
		font-size: 16px;
		color: #999999;

		navigator {
			color: #007AFF;
		}
	}
</style>
